/*
 * CollisionReader.cpp
 *
 *  Created on: 2009/02/05
 *      Author: Yongshik Cho
 */

#include "CollisionReader.h"
#include "../Exception.h"


const std::string CollisionReader::_NAME_TAG		= "name";
const std::string CollisionReader::_TYPE_TAG		= "type";
const std::string CollisionReader::_CLOSED_TAG		= "Closed";
const std::string CollisionReader::_POSITION_TAG	= "Position";
const std::string CollisionReader::_ROTATION_TAG	= "Rotation";
const std::string CollisionReader::_SCALE_TAG		= "Scale";
const std::string CollisionReader::_POINT_TAG		= "Point";
const std::string CollisionReader::_LINE_TAG		= "LINE";
const std::string CollisionReader::_QUADRATIC_TAG	= "QUADRATIC";
const std::string CollisionReader::_CUBIC_TAG		= "CUBIC";
const std::string CollisionReader::_TRUE_TAG		= "true";
const std::string CollisionReader::_FALSE_TAG		= "false";
const std::string CollisionReader::_ANGLE_TAG		= "angle";
const std::string CollisionReader::_X_TAG			= "x";
const std::string CollisionReader::_Y_TAG			= "y";


Collidable* CollisionReader::parse(TiXmlElement* element)
{
	/*
	 * 	<AltoCollision name='collision1' type='line' Closed='true'>
			<Position x=0 y=0/>
			<Rotation angle=0/>
			<Scale x=2 y=2/>

			<Point x=0 y=0/>
			<Point x=10 y=0/>
			<Point x=10 y=10/>
			<Point x=0 y=10/>
		</AltoCollision>
	 *
	 */

	char *sName, *sType, *sClosed;
	sName = (char*)element->Attribute(_NAME_TAG.c_str());
	sType = (char*)element->Attribute(_TYPE_TAG.c_str());
	sClosed = (char*)element->Attribute(_CLOSED_TAG.c_str());

	TiXmlElement* node = 0;
	node = element->FirstChildElement(_NAME_TAG.c_str()); if(node) sName = (char*)node->GetText();
	node = element->FirstChildElement(_TYPE_TAG.c_str()); if(node) sType = (char*)node->GetText();
	node = element->FirstChildElement(_CLOSED_TAG.c_str()); if(node) sClosed = (char*)node->GetText();

	Collidable* collidable = new Collidable();
	if(!collidable)
		AltoThrow(Exception::ERR_MEMORY);

	if(sName)
#ifdef UNICODE
	{
		TCHAR wstr[512]; memset(wstr, 0, sizeof(wstr));
		::MultiByteToWideChar(CP_UTF8, 0, sName, (int32)strlen(sName), wstr, 512);
		collidable->setName(wstr);
	}
#else
		collidable->setName(sName);
#endif

	if(sType)
	{
		if(_LINE_TAG == sType)
		{
		}
		else if(_QUADRATIC_TAG == sType)
		{
		}
		else if(_CUBIC_TAG == sType)
		{
		}
	}

	if(sClosed)
	{
		if(_TRUE_TAG == sClosed)
			collidable->setClosed(true);
		else if(_FALSE_TAG == sClosed)
			collidable->setClosed(false);
	}

	int32 x=0, y=0;
	double angle=0.f;

	for(TiXmlElement* point=element->FirstChildElement(_POINT_TAG.c_str());point;point=point->NextSiblingElement(_POINT_TAG.c_str()))
	{
		point->Attribute(_X_TAG.c_str(), &x);
		node = point->FirstChildElement(_X_TAG.c_str()); if(node) x = atoi(node->GetText());
		point->Attribute(_Y_TAG.c_str(), &y);
		node = point->FirstChildElement(_Y_TAG.c_str()); if(node) y = atoi(node->GetText());

		collidable->addPoint(Vector2D((float)x, (float)y));
	}

	TiXmlElement* position = element->FirstChildElement(_POSITION_TAG.c_str());
	if(position)
	{
		position->Attribute(_X_TAG.c_str(), &x);
		node = position->FirstChildElement(_X_TAG.c_str()); if(node) x = atoi(node->GetText());
		position->Attribute(_Y_TAG.c_str(), &y);
		node = position->FirstChildElement(_Y_TAG.c_str()); if(node) y = atoi(node->GetText());

		collidable->setPosition(Vector2D((float)x, (float)y));
	}

	TiXmlElement* rotation = element->FirstChildElement(_ROTATION_TAG.c_str());
	if(rotation)
	{
		rotation->Attribute(_ANGLE_TAG.c_str(), &angle);
		node = rotation->FirstChildElement(_ANGLE_TAG.c_str()); if(node) angle = atof(node->GetText());

		collidable->setRotation((float)angle);
	}

	return collidable;
}
